package com.nova.bluetooth.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Scroller;

import androidx.appcompat.widget.AppCompatTextView;

public class AutoScrollTextView extends AppCompatTextView {
    private Scroller mScroller;

    public AutoScrollTextView(Context context) {
        super(context);
        init(context);
    }

    public AutoScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mScroller = new Scroller(context);
        setMovementMethod(new ScrollingMovementMethod());
        setVerticalScrollBarEnabled(true);
        setHorizontalScrollBarEnabled(true);
        setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

    // 添加滑动到底部的方法
    public void scrollToBottom() {
        try {
            int scrollAmount = getLayout().getLineTop(getLineCount()) - getHeight();
            if (scrollAmount > 0) {
                mScroller.startScroll(getScrollX(), getScrollY(), 0, scrollAmount - getScrollY() + 50);
                postInvalidate();
            }
        } catch (Exception e) {
            Log.e("AutoScrollTextView", e.toString());
        }
    }

    @Override
    public boolean onTouchEvent(android.view.MotionEvent event) {
        return super.onTouchEvent(event);
    }


    public int lastStartIndex = -1;
    public int lastEndIndex = -1;
    /**
     * 在指定索引位置插入文本，并保存此次修改的索引
     *
     * @param text  要插入的文本
     * @param index 要插入的位置
     */
    public void insertTextAtIndex(String text, int index) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(getText());
        stringBuilder.insert(index, text);
        setText(stringBuilder);
        lastStartIndex = index;
        lastEndIndex = index + text.length();
    }

    /**
     * 在指定索引位置删除文本，并保存此次修改的索引
     *
     * @param start 起始索引
     * @param end   结束索引
     */
    public void deleteText(int start, int end) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(getText());
        stringBuilder.delete(start, end);
        setText(stringBuilder);
        lastStartIndex = start;
        lastEndIndex = start;  // 删除后的结束索引等于起始索引
    }

    /**
     * 在指定索引位置替换文本，并保存此次修改的索引
     *
     * @param text  要插入的文本
     * @param start 起始索引
     * @param end   结束索引
     */
    public void replaceText(int start, int end, String text) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(getText());
        stringBuilder.replace(start, end, text);
        setText(stringBuilder);
        lastStartIndex = start;
        lastEndIndex = start + text.length();
    }

    /**
     * 替换上一次修改的记录
     *
     * @param text 要替换的文本
     */
    public void replaceLastModification(String text) {
        if (lastStartIndex >= 0 && lastEndIndex >= 0) {
            replaceText(lastStartIndex, lastEndIndex, text);
        }
    }

    public void resetIndex(){
        lastStartIndex = -1;
        lastEndIndex = -1;
    }

    /**
     * 在文本末尾插入新文本，并保存此次修改的索引
     *
     * @param text 要插入的新文本
     */
    public void appendText(String text) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(getText());
        int startIndex = stringBuilder.length();
        stringBuilder.append(text);
        setText(stringBuilder);
        lastStartIndex = startIndex;
        lastEndIndex = startIndex + text.length();
    }

    public void appendText(String text, int color, boolean isBold) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(getText());
        int startIndex = stringBuilder.length();
        stringBuilder.append(text);

        // 设置文本颜色
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        stringBuilder.setSpan(colorSpan, startIndex, startIndex + text.length(), 0);

        // 设置文本加粗
        if (isBold) {
            StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
            stringBuilder.setSpan(boldSpan, startIndex, startIndex + text.length(), 0);
        }

        setText(stringBuilder);
        lastStartIndex = startIndex;
        lastEndIndex = startIndex + text.length();
    }
}
